<?php
/**
 * SHOP 
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

/*区域列表*/
if($_REQUEST['act']=='list'){
	$sql="select * from ".ly()."shop_region where r_del='0'";
	$res=$db->getAll($sql);
	$smarty->assign('res',$res);
	$smarty->display('quyu_list.html');
}elseif($_REQUEST['act']=='edit'){
	$r_id=$_REQUEST['r_id'];
	if($r_id){
		$sql="select r_name, r_zt,r_id from ".ly()."shop_region where r_id='{$r_id}'";
		$res=$db->getRow($sql);	
	}
	$smarty->assign('res',$res);
	$smarty->display('quyu_edit.html');
}elseif($_REQUEST['act']=='insert'){
	$r_id=$_POST['r_id'];
	$r_name=$_POST['r_name'];
	$r_zt=$_POST['r_zt'];
	if($r_id>0){
		$sql="update ".ly()."shop_region set r_zt='{$r_zt}' where r_id='{$r_id}'";	
	}else{
		$sql="insert into ".ly()."shop_region(r_name,r_zt,r_time) values('{$r_name}','{$r_zt}','".date('Y-m-d H:i:s')."')";
	}
	$res=$db->query($sql);
	if($res){
		 $href[]=array('text'=>"返回区域列表",'href'=>'quyu.php?act=list');
         sys_msg("操作成功！",$href);
	}else{
		$href[]=array('text'=>"重新编辑",'href'=>'history.back()');
        sys_msg("操作失败！",$href,false);
	}
}
elseif($_POST['act']=='name'){
	$r_name = $_POST['r_name'];
	$sql="select r_id from ".ly()."shop_region where r_name='{$r_name}' and r_del='0'";
	$res=$db->getOne($sql);
	if($res){
		echo 1;
	}else{
		echo 2;
	}
}elseif($_REQUEST['act']=='disable'){
	$r_id=isset($_POST['id'])?$_POST['id']:0;
	$vals=isset($_POST['val'])?$_POST['val']:0;
	if($vals==1){$zt='0';}else{$zt='1';}
		$sql="update ".ly()."shop_region set r_zt='{$zt}' where r_id='{$r_id}'";
		$res=$db->query($sql);
		if($res){
			echo "1";	
		}else{
			echo "2";
	}
	
}elseif($_REQUEST['act']=='del1'){
	$r_id=$_REQUEST['r_id'];
	$sql="select s_id from ".ly()."shop where r_id='{$r_id}' and del_status='0'" ;
	$res=$db->getALL($sql);
	if(empty($res)){
		$sql="update ".ly()."shop_region set r_del='1' where r_id='{$r_id}'";
		$res=$db->query($sql);
		if($res){
			echo '1';
		}
	}else{
		echo "所选区域下还有商家";
	}
	
}